//
//  UIView+DisclosureIndicator.m
//  FadeSDWebImage
//
//  Created by 冷悦 on 16/2/24.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "UIView+DisclosureIndicator.h"
#import <objc/runtime.h>

static void *DisclosureIndicatorEnable = &DisclosureIndicatorEnable;
static void *DisclosureIndicatorLength = &DisclosureIndicatorLength;
static void *DisclosureIndicatorColor = &DisclosureIndicatorColor;
static void *DisclosureIndicatorWidth = &DisclosureIndicatorWidth;

@implementation UIView (DisclosureIndicator)
@dynamic disclosureIndicatorEnable;
@dynamic disclosureIndicatorLength;
@dynamic disclosureIndicatorColor;
@dynamic disclosureIndicatorWidth;

#pragma mark - Privarte
- (CGPoint) calcCircleCoordinateWithCenter:(CGPoint)center
                                 withAngle:(CGFloat)angle
                                 andRadius:(CGFloat)radius{
    CGFloat x = radius * cosf(angle * M_PI / 180);
    CGFloat y = radius * sinf(angle * M_PI / 180);
    
    return CGPointMake(center.x + x, center.y + y);
}

- (void)createDisclosureIndicatorLineAtPoint:(CGPoint)refPoint{
    CGFloat radius = self.disclosureIndicatorLength;
    
    CGPoint topPoint = [self calcCircleCoordinateWithCenter:refPoint
                                                  withAngle:135
                                                  andRadius:radius];
    
    CGPoint bottomPoint = [self calcCircleCoordinateWithCenter:refPoint
                                                     withAngle:225
                                                     andRadius:radius];
    
    CAShapeLayer *line = [CAShapeLayer new];
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:topPoint];
    [path addLineToPoint:refPoint];
    [path addLineToPoint:bottomPoint];
    line.path = path.CGPath;
    line.lineWidth = self.disclosureIndicatorWidth;
    line.strokeColor = self.disclosureIndicatorColor.CGColor;
    line.fillColor = [UIColor clearColor].CGColor;
    line.lineJoin = kCALineJoinRound;
    line.lineCap = kCALineJoinRound;
    [self.layer addSublayer:line];
}

#pragma mark - Override
- (void)drawDisclosureIndicator{
    if (self.disclosureIndicatorEnable == YES) {
        DDLogInfo(@"in drawRect %s", __func__);
        CGRect rect = self.bounds;
        CGFloat centerY = CGRectGetMidY(rect);
        CGFloat x = CGRectGetMaxX(rect) - 5.0;
        CGPoint refPoint = CGPointMake(x, centerY);
        [self createDisclosureIndicatorLineAtPoint:refPoint];
    }
}

#pragma mark - Getters
- (BOOL)disclosureIndicatorEnable{
    NSNumber *disclosureIndicatorEnable = objc_getAssociatedObject(self, DisclosureIndicatorEnable);
    return disclosureIndicatorEnable == nil ? NO : [disclosureIndicatorEnable boolValue];
}

- (CGFloat)disclosureIndicatorLength{
    NSNumber *disclosureIndicatorLength = objc_getAssociatedObject(self, DisclosureIndicatorLength);
#ifndef kUIViewDisclosureIndicatorRadius
#define kUIViewDisclosureIndicatorRadius 10.0
#endif
    return disclosureIndicatorLength == nil ? kUIViewDisclosureIndicatorRadius : [disclosureIndicatorLength floatValue];
}

- (CGFloat)disclosureIndicatorWidth{
    NSNumber *disclosureIndicatorWidth = objc_getAssociatedObject(self, DisclosureIndicatorWidth);
#ifndef kUIViewDisclosureIndicatorLineWidth
#define kUIViewDisclosureIndicatorLineWidth 2.0
#endif
    return disclosureIndicatorWidth == nil ? kUIViewDisclosureIndicatorLineWidth : [disclosureIndicatorWidth floatValue];
}

- (UIColor *)disclosureIndicatorColor{
    UIColor *color = objc_getAssociatedObject(self, DisclosureIndicatorColor);
#ifndef kUIViewDisclosureIndicatorLineColor
#define kUIViewDisclosureIndicatorLineColor [UIColor lightGrayColor]
#endif
    return color == nil ? kUIViewDisclosureIndicatorLineColor : color;
}

#pragma mark - Setters
- (void)setDisclosureIndicatorEnable:(BOOL)disclosureIndicatorEnable{
    NSNumber *enable = [NSNumber numberWithBool:disclosureIndicatorEnable];
    objc_setAssociatedObject(self, DisclosureIndicatorEnable,
                             enable, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setDisclosureIndicatorLength:(CGFloat)disclosureIndicatorLength{
    NSNumber *length = [NSNumber numberWithFloat:disclosureIndicatorLength];
    objc_setAssociatedObject(self, DisclosureIndicatorLength,
                             length, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setDisclosureIndicatorColor:(UIColor *)disclosureIndicatorColor{
    objc_setAssociatedObject(self, DisclosureIndicatorColor,
                             disclosureIndicatorColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
